import os
import re

def extract_business_exceptions():
    # 定义匹配 BusinessException 的正则表达式
    pattern = re.compile(r'BusinessException\(\"(.*?)\"\)')

    # 用于存储提取到的键
    extracted_keys = set()

    # 遍历当前目录的所有文件
    for root, dirs, files in os.walk(os.getcwd()):
        for file in files:
            # 只处理文本文件（可以根据需要调整文件类型）
            if file.endswith(('.java', '.txt', '.py', '.js', '.vue')):
                file_path = os.path.join(root, file)
                try:
                    # 打开文件并提取内容
                    with open(file_path, 'r', encoding='utf-8') as f:
                        content = f.read()
                        matches = pattern.findall(content)
                        extracted_keys.update(matches)
                except Exception as e:
                    print(f"无法处理文件 {file_path}: {e}")

    # 生成 lang_zn.properties 文件
    with open('lang_zn.properties', 'w', encoding='utf-8') as lang_file:
        for key in sorted(extracted_keys):
            lang_file.write(f"{key}=""\n")

    print("lang_zn.properties 文件已生成！")

if __name__ == "__main__":
    extract_business_exceptions()
